﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using V2TEK.NET.SH.Silverlight.Charts.GanttHelper;
using System.Xml.Linq;
using V2TEK.NET.SH.Silverlight.GanttView.SharePointLists;
using System.Collections.Generic;
using System.Linq;
using V2TEK.NET.SH.Silverlight.Charts.Core.Schedule;
using System.Text.RegularExpressions;
using System.IO;
using V2TEK.NET.SH.Silverlight.Charts.TreeGridHelper;
using System.Threading;

namespace V2TEK.NET.SH.Silverlight.GanttView
{
    public class DDDataManager : DataManagerBase
    {
        protected override void ParseDataXML(XElement xml)
        {
            base.ParseDataXML(xml);

            var tasks = this.Data;

            for (int i = 0; i < tasks.Count; i++)
            {
                var task = tasks[i];
                string fileRef = task.ExtendAttributes["FileRef"] as string;
                if (fileRef.Contains("短计划"))
                {
                    tasks.RemoveAt(i);
                    i--;
                }
            }

            UpdateParent(tasks);

            this.Data = tasks;
        }

        public override void GetData(string listName, string viewName)
        {
            var itemsxml = XElement.Load("/V2TEK.NET.SH.Silverlight.GanttView;component/Gantt/ListItems.xml");
            ParseDataXML(itemsxml);

            Thread t = new Thread(() =>
            {
                Thread.Sleep(1000);
                this.Dispatcher.BeginInvoke(() =>
                {
                    RaiseGetDataCompleted();
                });
            });
            t.Start();
        }
        public override void GetData(string listName, string viewName, string zrcID, DynamicTaskExpander expander)
        {
            expander.HideExpander();
        }
    }

    public class RSDataManager : DataManagerBase
    {
        protected override void ParseDataXML(XElement xml)
        {
            base.ParseDataXML(xml);

            var tasks = this.Data;

            for (int i = 0; i < tasks.Count; i++)
            {
                var task = tasks[i];
                string fileRef = task.ExtendAttributes["FileRef"] as string;
                if (fileRef.Contains("短计划"))
                {
                    tasks.RemoveAt(i);
                    i--;
                }
            }

            UpdateParent(tasks);

            this.Data = tasks;
        }

        public override void GetData(string listName, string viewName)
        {
            if (Fields == null)
                Fields = new Dictionary<string, SharePointField>();
            else
                Fields.Clear();
            if (ViewFields == null)
                ViewFields = new List<string>();
            else
                ViewFields.Clear();
            var viewxml = XElement.Load("/V2TEK.NET.SH.Silverlight.GanttView;component/Gantt/ListAndView.xml");
            ViewFieldXml = viewxml;
            ParseFields(viewxml, Fields, ViewFields);
            var result = from c in Fields where ViewFields.Contains(c.Key) select c;
            List<KeyValuePair<string, SharePointField>> list = new List<KeyValuePair<string, SharePointField>>(result);
            Fields.Clear();
            foreach (var item in list)
            {
                Fields.Add(item.Key, item.Value);
            }

            var itemsxml = XElement.Load("/V2TEK.NET.SH.Silverlight.GanttView;component/Gantt/ListItems.xml");
            ParseDataXML(itemsxml);

            Thread t = new Thread(() =>
                {
                    Thread.Sleep(1000);
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        RaiseGetDataCompleted();
                    });
                });
            t.Start();
        }
        public override void GetData(string listName, string viewName, string zrcID, DynamicTaskExpander expander)
        {
            expander.HideExpander();
        }
    }

    public class JNDataManager : DataManagerBase
    {
        protected override void ParseDataXML(XElement xml)
        {
            base.ParseDataXML(xml);

            var tasks = this.Data;

            for (int i = 0; i < tasks.Count; i++)
            {
                var task = tasks[i];
                string fileRef = task.ExtendAttributes["FileRef"] as string;
                if (fileRef.Contains("短计划"))
                {
                    tasks.RemoveAt(i);
                    i--;
                }
            }

            UpdateParent(tasks);

            this.Data = tasks[0].Children;
        }
    }
}
